package com.xyyl.patient.service.doctor;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xyyl.patient.core.support.Pager;
import com.xyyl.patient.entity.doctor.Disease;
import com.xyyl.patient.entity.doctor.Doctor;
import com.xyyl.patient.entity.doctor.DoctorPatientRelation;
import com.xyyl.patient.entity.order.ConsultOrder;
import com.xyyl.patient.entity.order.PhoneOrder;
import com.xyyl.patient.entity.question.DoctorEvaluate;
import com.xyyl.patient.entity.question.QuickQuestion;
import com.xyyl.patient.mapper.doctor.DiseaseMapper;
import com.xyyl.patient.mapper.doctor.DoctorMapper;
import com.xyyl.patient.mapper.doctor.DoctorPatientRelationMapper;
import com.xyyl.patient.mapper.order.ConsultOrderMapper;
import com.xyyl.patient.mapper.order.PhoneOrderMapper;
import com.xyyl.patient.mapper.question.DoctorEvaluateMapper;
import com.xyyl.patient.mapper.question.QuickQuestionMapper;
/**
 * @ClassName: UserServiceImp
 * @Description: 患者信息Service
 * @author chengjun
 * @date 2017年8月17日
 *
 */
@Service("DoctorService")
public class DoctorServiceImp implements DoctorService {

	@Autowired
	private DoctorMapper doctorMapper;
	@Autowired
	private DiseaseMapper diseaseMapper;

	@Autowired
	private DoctorEvaluateMapper doctorEvaluateMapper;
	@Autowired
	private DoctorPatientRelationMapper doctorPatientRelationMapper;
	@Autowired
	private ConsultOrderMapper consultOrderMapper;
	
	@Autowired
	private QuickQuestionMapper quickQuestionMapper;

	@Autowired
	private PhoneOrderMapper phoneOrderMapper;
	@Override
	public List<Doctor> getDoctor(Pager pager){

		List<Doctor> doctors = null;
		try {
			doctors = doctorMapper.queryForPage(pager);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return doctors;
	}
	@Override
	public List<Map<String,Object>> getHosp(){
		return  doctorMapper.getHosp();
	}
	@Override
	public List<Map<String,Object>> getDept(Integer hospitalid){
		if(hospitalid ==null){
			return  doctorMapper.getDept();
		}else {
			return  doctorMapper.getDeptByHosId(hospitalid);
		}

	}

	@Override
	public String  getDoctorIMEI(Integer doctorId){
		return doctorMapper.getDoctorIMEI(doctorId);
	}
	@Override
	public List<Disease> getDisease(){
		List<Disease> diseases = diseaseMapper.selectList(new Disease());
		return diseases;

	}
	@Override
	public Doctor getDoctorById(Integer doctorId) {
		Doctor doctor = doctorMapper.selectOne(doctorId);
		return doctor;
	}
	@Override
	public Doctor getDoctorinfo(Integer doctorId) {
		Doctor doctor = doctorMapper.getDoctorinfo(doctorId);
		return doctor;
	}
	@Override
	public List<Disease> getDiseaseByDoctorId(Integer doctorId){
		List<Disease> diseases =diseaseMapper.getDiseaseByDoctorId(doctorId);
		return diseases;
	}
	@Override
	public List<DoctorEvaluate> getDoctorEvaluate(Pager pager){

		return doctorEvaluateMapper.queryForPage(pager);
	}
	@Override
	public Integer saveDoctorEvaluate(DoctorEvaluate doctorEvaluate){
		//插入 医生评价表
		Integer result = doctorEvaluateMapper.insert(doctorEvaluate);
		Integer doctorEvaluateId  =doctorEvaluate.getId();
		if(result !=null &&doctorEvaluateId !=null){
			//获取医生的平均评分
			DoctorEvaluate doctorEvaluate1 = doctorEvaluateMapper.selectByDoctorId(doctorEvaluate.getDoctorid());
 			double  score = doctorEvaluate1.getScore().doubleValue()/doctorEvaluate1.getType().doubleValue();
			java.math.BigDecimal bigDecimal = new java.math.BigDecimal(score).setScale(1, BigDecimal.ROUND_HALF_UP);
			Doctor doctor = new Doctor();
			doctor.setId(doctorEvaluate.getDoctorid());
			doctor.setScore(bigDecimal);
			//更新医生信息
			doctorMapper.update(doctor);
			//更新订单消息1：快速提问；2：图文咨询；3：电话咨询
			if(doctorEvaluate.getType()==1){
				QuickQuestion quickQuestion =new QuickQuestion();
				quickQuestion.setIscommented(1);
				quickQuestion.setQuestionid(doctorEvaluate.getServiceid());
				quickQuestion.setQstatus(2);
				quickQuestionMapper.update(quickQuestion);

			}else if (doctorEvaluate.getType()==2){
				ConsultOrder consultOrder =new ConsultOrder();
				consultOrder.setId(doctorEvaluate.getServiceid());
				consultOrder.setIscommented(1);
				consultOrderMapper.update(consultOrder);
			}else if (doctorEvaluate.getType()==3){
				PhoneOrder phoneOrder = new PhoneOrder();
				phoneOrder.setId(doctorEvaluate.getServiceid());
				phoneOrder.setReviewstatus(1);
				phoneOrderMapper.update(phoneOrder);
			}

			return result;

		}else {
			return 0;
		}
	}
	@Override
	public Integer addDoctorFriends(DoctorPatientRelation doctorPatientRelation){
		Integer result = doctorPatientRelationMapper.insert(doctorPatientRelation);
		return result;
	}
	@Override
	public DoctorPatientRelation getDoctorFriends(DoctorPatientRelation doctorPatientRelation){
		DoctorPatientRelation doctorPatientRelation1 =doctorPatientRelationMapper.selectOne(doctorPatientRelation);
		return doctorPatientRelation1;
	}
	@Override
	public Integer deleteIM(Integer id){
		Integer i  =doctorPatientRelationMapper.delete(id);
		return i;
	}
	   
	    
	@Override
	public List<Map<String, Object>> getBookTimeByDoctorId(Integer doctorid) {
		return doctorMapper.getBookTimeByDoctorId(doctorid);
	}
	
	 
	@Override
	public String getDoctorDeviceTokensById(Integer doctorId) {
		return doctorMapper.getDoctorDeviceTokensById(doctorId);
	}
	
	    
	@Override
	public List<Doctor> queryDoctorListOfSpeedPhoneOrder() {
		return doctorMapper.queryDoctorListOfSpeedPhoneOrder();
	}

	@Override
	public Integer deleteByID(Integer id) {
		Integer i = doctorPatientRelationMapper.deleteByID(id);
		return i;
	}

	@Override
	public void deleteIMByParameter(DoctorPatientRelation doctorPatientRelation) {
		doctorPatientRelationMapper.deleteIMByParameter(doctorPatientRelation);
	}

	@Override
	public void deleteIMByPatientid(Map<String, Object> map) {
		doctorPatientRelationMapper.deleteIMByPatientid(map);
	}

	@Override
	public void deleteDPRByPatientid(Map<String, Object> map) {
		doctorPatientRelationMapper.deleteDPRByPatientid(map);
	}

	@Override
	public Integer saveAndDelete(Map<String, Object> map, DoctorPatientRelation doctorPatientRelation) {
		// 删除聊天列表
		doctorPatientRelationMapper.deleteIMByPatientid(map);
		// 删除关注的医生
		doctorPatientRelationMapper.deleteDPRByPatientid(map);
		// 添加新的医生
		Integer result = doctorPatientRelationMapper.insert(doctorPatientRelation);
		return result;
	}

	@Override
	public Integer getFollow(Map<String, Object> map) {
		return doctorPatientRelationMapper.getFollow(map);
	}

	@Override
	public Integer saveFollow(Map<String, Object> map) {
		return doctorPatientRelationMapper.saveFollow(map);
	}

	@Override
	public Integer deleteFollow(Integer id) {
		return doctorPatientRelationMapper.deleteFollow(id);
	}

	@Override
	public List<Doctor> followDoctorList(Integer patientid) {
		return doctorPatientRelationMapper.followDoctorList(patientid);
	}

	@Override
	public void updateDoctorConcern(Integer docotrid, Integer count) {
		Map<String, Object> map = new HashMap<>();
		map.put("docotrid", docotrid);
		map.put("count", count);
		doctorMapper.updateDoctorConcern(map);
	}

	@Override
	public String queryDoctorMoblieById(Integer doctorid) {
		return doctorMapper.queryDoctorMoblieById(doctorid);
	}

	@Override
	public List<DoctorEvaluate> getMyEvaluate(Integer patientid) {
		// TODO Auto-generated method stub  
		return doctorEvaluateMapper.getMyEvaluate(patientid);
	}



	//@Override
	//public List<UserVo> selectList(Pager pager) {
    //
	//	List<UserVo> userVos = Lists.newArrayList();
    //
	//	List<User> users = userMapper.queryForPage(pager);
    //
	//	if (users != null && !users.isEmpty()) {
	//		UserVo userVo = null;
	//		for (User user : users) {
	//			userVo = new UserVo();
	//			BeanUtils.copyProperties(user, userVo);
    //
	//			userVo.setSex(AppEnum.getName(user.getSex()));
    //
	//			userVos.add(userVo);
	//		}
    //
	//		pager.setData(userVos);
	//	}
	//	return userVos;
	//}

	//@Override
	//public UserVo queryUserDetailById(Integer userid) {
    //
	//	UserVo userVo = null;
	//	User user = userMapper.queryUserByUserid(userid);
    //
	//	if (user != null) {
	//		userVo = new UserVo();
    //
	//		BeanUtils.copyProperties(user, userVo);
	//		userVo.setSex(AppEnum.getName(user.getSex()));
	//	}
	//	return userVo;
	//}

}
